<?php

	require_once getcwd() . '/service.impl/ShotDayManager.php';
	require_once getcwd(). '/library/Log.php';
	
	class UsersController extends MyController {
		//propriete manager
		private $manager;
		
		public function __construct(){
			$this->manager = new ShotDayManager();
		}
		
		
		public function getAction($request){
			$data = array();
			//echo $_SERVER['REQUEST_METHOD'];
			//verb/uid/
			if(isset($request->url_elements[2])) {
				$user_id =  $request->url_elements[2]; //cast (int)
				
				if(isset($request->url_elements[3])) {
					
					switch($request->url_elements[3]) {
						case 'y': 
								$data = $this->getShotsYear($user_id,$request->url_elements);
								break;
						case 'm' : 
								$data = $this->getShotsMonth($user_id,$request->url_elements);
								break;
						case 'my' :
								$data = $this->getShotsMonthofYear($user_id,$request->url_elements);
								break;
						case 'dm' : 
							$data = $this->getShotsDayofMonth($user_id,$request->url_elements);
							break;
						case 'dmy' : 
							$data = $this->getShotDate($user_id,$request->url_elements);		
							break;
							
						default : break;
					}
				}
				// getphoto
				else {
					
					$manager = $this->manager;
					$shot = $manager->get($user_id);
					if($shot!=null && $shot!= false){
						$data['message']=$shot;
						$data['code'] = 200;
					}								
					else{					
						$data["message"] = "No object";
						$data['code'] = 204;
					}
				}
			}
			else {
				$data["message"] = "bad request";
				$data['code'] = 400;
			}
			return $data;		
		}		
	
			//enregistre le fichier en base
		private function saveShot($filename,$uid,$post){
			
			//description
			$description = "";
			$city ="";
			$country="";
			$tag0 = $tag1 = $tag2 = "";
						
			if(isset($post['description'])){
				$description = 	$post['description'];
			}
            //localisation
            if(isset($_POST['city'])){
            	$city = $_POST['city'];
            }
			if(isset($_POST['country']))
            	$country = $_POST['country'];
			if(isset($_POST['keyF'])){
				$tagO = $POST['keyF'];
			}
			if(isset($_POST['keyS'])){
				$tag1 = $POST['keyS'];
			}
			if(isset($_POST['keyT'])){
				$tag2 = $POST['keyT'];
			}
			
			$shotModel = new DayShot('',$filename,$uid,$description,$city,$country);	
			$shotModel->key_first = $tag0;
			$shotModel->key_second = $tag1;
			$shotModel->key_third = $tag2;
			
			//var_dump($shotModel);
			//verifier localisation
			//if(isset(post['city'] && post['country'])){}
				
			$manager = $this->manager; 
			$res = $manager->insert($shotModel);				
				
			return $res;
			
		}
		
		private function postUpdateShot($uid){
			$description = "";
			$city ="";
			$country="";
			$tag0 = $tag1 = $tag2 = "";
			$filename = "";
			
			
			if(isset($post['description'])){
				$description = 	$post['description'];
			}
			//localisation
			if(isset($_POST['city'])){
				$city = $_POST['city'];
			}
			if(isset($_POST['country']))
				$country = $_POST['country'];
			if(isset($_POST['keyF'])){
				$tagO = $POST['keyF'];
			}
			if(isset($_POST['keyS'])){
				$tag1 = $POST['keyS'];
			}
			if(isset($_POST['keyT'])){
				$tag2 = $POST['keyT'];
			}
			if(isset($_POST['filename']))
				$filename = $_POST['filename'];
				
			$shotModel = new DayShot('',$filename,$uid,$description,$city,$country);
			$shotModel->key_first = $tag0;
			$shotModel->key_second = $tag1;
			$shotModel->key_third = $tag2;
			
			$manager = $this->manager;
			$res = $manager->update($shotModel);
			$data;
			if(res == 1){
				$data['code'] = 200;
				$data['message'] = "Data updated";
				
			}
			else {
				$data['code'] = 500;
				$data['message'] = "Internal error";
			}
			return $data;
			
		}
		/*
		 * ID Utilisateur
		 * plus information de la photo
		 * 
		 * question (name du imput type=file)
		 */
		public function postAction($request){
			Log::write("UsersController::postAction");
			$data = $request->parameters;			

			//Verifier l'existence d'uid utilisateur
			if(isset($request->url_elements[2])){
				$uid= $request->url_elements[2];
				
				if(isset($_FILES['file'])){
					Log::write("insert data");
					$data = $this->postShotofDay($uid);
				}
				if(isset($request->url_elements[3]) and $request->url_elements[3] == "update"){
					Log::write("update data");
					$data = $this->postUpdateShot($uid);
				}
				else {
					$data['message']="No query";
					$data['code'] = 400;
				}
			}
			else  {
				$data['message'] = 'Unknown request';
				$data['code'] = 400;
			}
			Log::write("fin UsersController::postAction");
			return $data;	
			
		}	
		//my
		private function getShotsMonthofYear($uid,$elements){
			if(isset($elements[4]) && isset($elements[5])){
				$month = (int) $elements[5];
				$year = (int) $elements[4];
				if($month <1 || $month >12 || $year<0) {
					$data['message'] = "Bad request : unknown parameters";
					$data['code'] = 400;
				}
				else{
					$manager = $this->manager;
					$data['code'] = 200;
					$data['message'] = $manager->getObjectsByDate($uid,$year,$month,'');
				} 
					
							
			}
			else $data['message'] = "Requete invalide";
			
			return $data;
		}
		//m
		private function getShotsMonth($uid,$elements){
			if(isset($elements[4])){
				if(((int)$elements[4])<1 || ((int)$elements[4])>12){
					$data['message'] = "Bad request : unknown parameters";
					$data['code'] = 400;
				}
				else {
					$manager = $this->manager;
					$data['message'] = $manager->getObjectsByDate($uid,'',(int)$elements[4],'');
					$data['code'] = 200;
				}
			}
			else {
				$data['message'] = "Bad request";
				$data['code'] = 400;
			}
				
			return $data;
		}
		//y
		private function getShotsYear($uid,$elements){
			if(isset($elements[4])){
				$y = (int)$elements[4];
				$manager = $this->manager;
				$data['message'] = $manager->getObjectsByDate($uid,$y,'','');
				$data['code'] = 200;
			}
			else {
				$data['message'] = "Bad request";
				$data['code'] = 400;
			}
			
			return $data;
		}
		//dm
		private function getShotsDayofMonth($uid,$elements){
			if(isset($elements[4]) && isset($elements[5])){
				$day = (int) $elements[5];
				$month = (int) $elements[4];
				if($month <1 || $month >12 || $day<1 || $day>31 || ($month==2 && $day >29)) {
					$data['message'] = "Bad request : unknown parameters";
					$data['code'] = 400;
				}
				else{
					$manager = $this->manager;
					$data['message'] = $manager->getObjectsByDate($uid,'',$month,$day);
					$data['code'] = 200;
				}
					
			}
			else {
				$data['message'] = "Bad request";
					$data['code'] = 400;
			}
				
			return $data;
		}
		//dmy
		private function getShotDate($uid,$elements){
			if(isset($elements[4]) && isset($elements[5]) && isset($elements[6])){
				$y=(int)$elements[4];$m=$elements[5];$d=$elements[6];
				if($m <1 || $m >12 || $d<1 || $d>31 || ($m==2 && $d >29)){
					$data['message'] = "Bad request";
					$data['code'] = 400;
				}
				else{
					$manager = $this->manager;
					$data['message'] = $manager->getObjectsByDate($uid,$year,$month,$day);
					$data['code'] = 200;
				} 
					
			}
			else {
				
			}
			return $data;
			
		}
		
		private function postShotofDay($uid){
			$vardate = date("d/m/Y-H:i:s");
			error_log($vardate ." - UsersController postShotofDay : \n" , 3, "log/mes-erreurs.log");
			$dir ="img/" . $uid."/";
				
			if(!file_exists($dir))
				mkdir($dir);
			//ajouter une contrainte sur l'extension du fichier
			$extension = strchr($_FILES['file']['name'], '.');
			$filename = $dir . time() . $extension;
				
			$target = $filename;
				
			if(move_uploaded_file($_FILES['file']['tmp_name'],$target)){
				
				error_log($vardate ." - UsersController postShotofDay filename : ".$filename ." \n" , 3, "log/mes-erreurs.log");
				error_log($vardate ." - UsersController postShotofDay uid : ".$uid ." \n" , 3, "log/mes-erreurs.log");				
				$res= $this->saveShot($filename,$uid,$_POST);
				error_log($vardate ." - UsersController postShotofDay : Valeur de res: ".$res ." \n" , 3, "log/mes-erreurs.log");
				if($res==1){
					$data["message"] = "This data was submitted";
					$data['code'] = 201;
				}
				else {
					$data["message"] = "Saving error";
					$data['code'] = 500;
				}
		
			}
			else {
				$data['message'] = "Bad request : unknown parameters";
				$data['code'] = 400;
			}
		
			return $data;
	
		}
	}

?>